package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.domain.ShBannerPicture;
import com.ruoyi.system.mapper.ShBannerPictureMapper;
import com.ruoyi.system.service.IShBannerPictureService;
import com.ruoyi.system.utils.TencentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShBannerPictureServiceImpl implements IShBannerPictureService {
    @Autowired
    private ShBannerPictureMapper shBannerPictureMapper;

    @Override
    public ShBannerPicture selectShBannerPictureById(Long id) {
        return shBannerPictureMapper.selectShBannerPictureById(id);
    }

    @Override
    public List<ShBannerPicture> selectShBannerPictureList(ShBannerPicture shBannerPicture) {
        return shBannerPictureMapper.selectShBannerPictureList(shBannerPicture);
    }

    @Override
    public int insertShBannerPicture(ShBannerPicture shBannerPicture) {
        shBannerPicture.setCreateTime(DateUtils.getNowDate());
        return shBannerPictureMapper.insertShBannerPicture(shBannerPicture);
    }

    @Override
    public int updateShBannerPicture(ShBannerPicture shBannerPicture) {
        return shBannerPictureMapper.updateShBannerPicture(shBannerPicture);
    }

    @Override
    public int deleteShBannerPictureByIds(Long[] ids) {
        TencentUtils tencentUtils = TencentUtils.getInstance();
        for (Long id : ids) {
            ShBannerPicture item = shBannerPictureMapper.selectShBannerPictureById(id);
            if (null != item.getProductPic() && item.getProductPic().indexOf("aliyuncs.com/") > 0) {
                String[] keyAry = item.getProductPic().split("aliyuncs.com/");
                tencentUtils.del(keyAry[1]);
            }
            shBannerPictureMapper.deleteShBannerPictureById(id);
        }
        return ids.length;
    }

    @Override
    public int deleteShBannerPictureById(Long id) {
        TencentUtils tencentUtils = TencentUtils.getInstance();
        ShBannerPicture item = shBannerPictureMapper.selectShBannerPictureById(id);
        if (null != item.getProductPic() && item.getProductPic().indexOf("aliyuncs.com/") > 0) {
            String[] keyAry = item.getProductPic().split("aliyuncs.com/");
            tencentUtils.del(keyAry[1]);
        }
        return shBannerPictureMapper.deleteShBannerPictureById(id);
    }
}
